package com.bjpowernode.controller;


import com.bjpowernode.beans.Value;
import com.bjpowernode.dto.ResultDTO;
import com.bjpowernode.exception.CRUDException;
import com.bjpowernode.services.ValueServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@RestController
@RequestMapping("/DictionaryValue")
public class ValueController {

    @Autowired
    ValueServices valueServices;

    @Autowired
    MessageSource messageSource;

    @RequestMapping("getAll.action")
    public ArrayList<Value> getAll() {
        return valueServices.getAll();
    }

    @RequestMapping("add.action")
    public ResultDTO add(@Valid Value value, BindingResult bindingResult) throws CRUDException {
        ResultDTO resultDTO=new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);

        if(bindingResult.hasErrors()){
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
                String defaultMessageKey = fieldError.getDefaultMessage();
                String message = messageSource.getMessage(defaultMessageKey, null, Locale.ENGLISH);
                throw new CRUDException(message);
            }
        }

        boolean add = valueServices.add(value);
        if(!add){
            throw new CRUDException("添加没有任何影响行数");
        }
        return resultDTO;
    }

    @RequestMapping("del.action")
    public ResultDTO del(@RequestBody String[] ids) throws CRUDException {
        ResultDTO resultDTO=new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        boolean del = valueServices.del(ids);
        if(!del){
            throw new CRUDException("删除没有任何影响行数");
        }
        return resultDTO;
    }

    @RequestMapping("editBefore.action")
    public Value get(String id) {
        return valueServices.get(id);
    }


    @RequestMapping("edit.action")
    public ResultDTO edit(@Valid Value value, BindingResult bindingResult) throws CRUDException {
        ResultDTO resultDTO=new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);

        if(bindingResult.hasErrors()){
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
                String defaultMessageKey = fieldError.getDefaultMessage();
                String message = messageSource.getMessage(defaultMessageKey, null, Locale.ENGLISH);
                throw new CRUDException(message);
            }
        }

        boolean edit = valueServices.edit(value);
        if(!edit){
            throw new CRUDException("修改没有任何影响行数");
        }
        return resultDTO;
    }


}
